class ApiResponse<T> {
  final String msg;
  final int code;
  final T? data;

  ApiResponse({
    required this.msg,
    required this.code,
    this.data,
  });

  factory ApiResponse.fromJson(
      Map<String, dynamic> json, T Function(dynamic)? fromJson) {
    dynamic dataValue;

    // 如果有 data 字段，使用 data 字段
    if (json.containsKey('data')) {
      dataValue = json['data'];
    } else {
      // 如果没有 data 字段，直接返回整个响应对象
      dataValue = json;
    }

    final data = fromJson != null && dataValue != null
        ? fromJson(dataValue)
        : dataValue as T?;

    return ApiResponse(
      msg: json['msg'] as String,
      code: json['code'] as int,
      data: data,
    );
  }

  Map<String, dynamic> toJson() => {
        'msg': msg,
        'code': code,
        'data': data,
      };

  bool get isSuccess => code == 200;
}
